package com.zjdiepu.www.manage.api.partner;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.annotation.Add;
import com.zjdiepu.www.base.annotation.Update;
import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.PageVo;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.status.ThirdBusiStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.ibiz.cache.ICachedDictionaryService;
import com.zjdiepu.www.ibiz.cw.ICwRemitCarService;
import com.zjdiepu.www.ibiz.dz.IDzLoanMiddleBankService;
import com.zjdiepu.www.ibiz.partner.IThLendingService;
import com.zjdiepu.www.ibiz.partner.IThRepaymentScheduleService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessService;
import com.zjdiepu.www.model.dz.dto.DzLoanMiddleBank;
import com.zjdiepu.www.model.partner.dto.ThLending;
import com.zjdiepu.www.model.partner.dto.ThRepaymentSchedule;
import com.zjdiepu.www.model.partner.ext.ThRepaymentScheduleExt;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.yw.request.YwBusinessQueryRequest;

/**
 * 第三方募集信息Controller层<br/>
 * @author zhangzhengjian<br/>
 * @time 2017年09月28日 10时13分<br/>
 * @since 1.0<br/>
 */
@RestController
@RequestMapping(value= "ThRepaymentScheduleController", produces="application/json;charset=UTF-8")
@Api(tags = {"第三方募集信息"}, produces="application/json;charset=UTF-8")
public class ThRepaymentScheduleController extends BaseController {
    @Resource
    private IThRepaymentScheduleService thRepaymentScheduleService;
    @Resource
    private IYwBusinessService ywBusinessService;
    @Resource
    private ICwRemitCarService cwRemitCarService;
    @Resource
    private ICachedDictionaryService cachedDictionaryService;
    @Resource
    private IDzLoanMiddleBankService dzloanMiddleBankService;
    @Resource
    private IThLendingService thLendingService;

    /**
     * @author zhangzhengjian <br/>
     * @Title: save  <br/>
     * @Description: 新增第三方募集信息 <br/>
     * @param thRepaymentSchedule
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "新增第三方募集信息", response = Boolean.class, httpMethod="POST", tags={"新增"})
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public ApiResponse save(@Validated(Add.class) @ModelAttribute ThRepaymentSchedule thRepaymentSchedule) {
    	AbstractSysUser currentUser = getCurrentUser();
    	thRepaymentSchedule.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	thRepaymentSchedule.setModifierName(currentUser.getName());
        thRepaymentScheduleService.saveUpdateAndDelete(thRepaymentSchedule, OperateType.add);
        return ApiResponse.success(true);
    }

    /**
     * @author zhangzhengjian <br/>
     * @Title: remove  <br/>
     * @Description: 删除第三方募集信息<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "删除第三方募集信息", response = Boolean.class, httpMethod="POST", tags={"删除"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "remove", method = RequestMethod.POST)
    public ApiResponse remove(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	thRepaymentScheduleService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.DELETED.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author zhangzhengjian <br/>
     * @Title: disable  <br/>
     * @Description: 禁用第三方募集信息<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "禁用第三方募集信息", response = Boolean.class, httpMethod="POST", tags={"禁用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "disable", method = RequestMethod.POST)
    public ApiResponse disable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	thRepaymentScheduleService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.INVALID.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author zhangzhengjian <br/>
     * @Title: enable <br/>
     * @Description: 启用第三方募集信息<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "启用第三方募集信息", response = Boolean.class, httpMethod="POST", tags={"启用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "enable", method = RequestMethod.POST)
    public ApiResponse enable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	thRepaymentScheduleService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.NORMAL.getCode(), ids);
        return ApiResponse.success(true);
    }

    /**
     * @author zhangzhengjian <br/>
     * @Title: modify  <br/>
     * @Description: 修改第三方募集信息 <br/>
     * @param thRepaymentSchedule
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "修改第三方募集信息", response = Boolean.class, httpMethod="POST", tags={"修改"})
    @RequestMapping(value = "modify", method = RequestMethod.POST)
    public ApiResponse modify(@Validated(Update.class) @ModelAttribute ThRepaymentSchedule thRepaymentSchedule) {
    	AbstractSysUser currentUser = getCurrentUser();
    	thRepaymentSchedule.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	thRepaymentSchedule.setModifierName(currentUser.getName());
    	thRepaymentScheduleService.saveUpdateAndDelete(thRepaymentSchedule, OperateType.edit);
        return ApiResponse.success(true);
    }

    /**
     * @author zhangzhengjian <br/>
     * @Title: getById  <br/>
     * @Description: 主键查询第三方募集信息<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "主键查询第三方募集信息", response = ThRepaymentSchedule.class, httpMethod="POST", tags={"主键查询"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "getById", method = RequestMethod.POST)
    public ApiResponse getById(String id){
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
       ThRepaymentSchedule thRepaymentSchedule = thRepaymentScheduleService.selectById(id);
       if(null == thRepaymentSchedule) {
    	   return ApiResponse.fail(ResultFlagType.MISSING);
       }
       return ApiResponse.success(thRepaymentSchedule);
    }
    
    /**
     * @author zhangzhengjian <br/>
     * @Title: list  <br/>
     * @Description: 条件查询第三方募集信息 <br/>
     * @param thRepaymentScheduleExt
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "条件查询第三方募集信息",response = ThRepaymentSchedule.class, httpMethod="POST", tags={"列表"})
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public ApiResponse list(@ModelAttribute ThRepaymentScheduleExt thRepaymentScheduleExt){
        thRepaymentScheduleExt.notDeleted();
        PageVo page = getPage(thRepaymentScheduleExt);
        int records = thRepaymentScheduleService.countByExt(thRepaymentScheduleExt);
        page.setRecords(records);
        if (records > 0) {
        	String busiId="";
        	YwBusiness ywBusiness=null;
        	ThLending thLending=null;
        	DzLoanMiddleBank dzLoanMiddleBank=null;
            List<ThRepaymentSchedule> dpBaseDataDictionaries = thRepaymentScheduleService.selectByExt(thRepaymentScheduleExt);
            for (ThRepaymentSchedule thRepaymentSchedule : dpBaseDataDictionaries) {
            	busiId=thRepaymentSchedule.getBusiId();
            	ywBusiness=ywBusinessService.selectById(busiId);
            	if(ywBusiness!=null){
            		thRepaymentSchedule.setCustomerName(ywBusiness.getCustomerName());
                	thRepaymentSchedule.setCustomerNo(ywBusiness.getCustomerNo());
            	}
            	
            	
            	dzLoanMiddleBank=new DzLoanMiddleBank();
            	dzLoanMiddleBank.setBusiId(busiId);
            	dzLoanMiddleBank=dzloanMiddleBankService.selectRefined(dzLoanMiddleBank);
            	if(dzLoanMiddleBank!=null){
            		thRepaymentSchedule.setCardnoRefund(dzLoanMiddleBank.getCardnoRefund());
            	}
            	
            	thLending=new ThLending();
            	thLending.setBusiId(busiId);
            	thLending=thLendingService.selectRefined(thLending);
            	if(thLending!=null){
            		thRepaymentSchedule.setBankLoanDisplay(thLending.getLoanBankName());
            	}
			}
            page.setRows(dpBaseDataDictionaries);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }
    @ApiOperation(value = "条件查询业务信息",response = YwBusiness.class, httpMethod="POST", tags={"多表查询"})
    @RequestMapping(value = "multiList", method = RequestMethod.POST)
    public ApiResponse multiList(@Validated @ModelAttribute YwBusinessQueryRequest ywBusinessQueryRequest){
        PageVo page = getPage(ywBusinessQueryRequest);
        ywBusinessQueryRequest.setOrderSql(" B.created desc");
        ywBusinessQueryRequest.setThirdBusiStatus(ThirdBusiStatus.FANG_KUAN_CHENG_GONG.getCode());
        int records = thRepaymentScheduleService.countMultiByExt(ywBusinessQueryRequest);
        page.setRecords(records);
        if (records > 0) {
        	String busiId="";
        	DzLoanMiddleBank dzLoanMiddleBank=null;
        	ThLending thLending=null;
            List<ThRepaymentSchedule> scheduleList = thRepaymentScheduleService.selectMultiByExt(ywBusinessQueryRequest);
            for (ThRepaymentSchedule thRepaymentSchedule : scheduleList) {
            	busiId=thRepaymentSchedule.getBusiId();
            	dzLoanMiddleBank=new DzLoanMiddleBank();
            	dzLoanMiddleBank=dzloanMiddleBankService.selectRefined(dzLoanMiddleBank);
            	if(dzLoanMiddleBank!=null){
            		thRepaymentSchedule.setCardnoRefund(dzLoanMiddleBank.getCardnoRefund());
            	}
            	thLending=new ThLending();
            	thLending.setBusiId(busiId);
            	thLending=thLendingService.selectRefined(thLending);
            	if(thLending!=null){
            		thRepaymentSchedule.setBankLoanDisplay(thLending.getLoanBankName());
            	}
			}
            page.setRows(scheduleList);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }

}
